package org.tomac.fix.example.fix42nordic;

import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.tomac.io.IIOPlexor;
import org.tomac.io.ITimerTask;
import org.tomac.io.fix.FixInitiator;
import org.tomac.io.fix.FixURL;
import org.tomac.io.fix.IFixApplication;
import org.tomac.io.fix.IFixSession;
import org.tomac.io.fix.IFixURL;
import org.tomac.io.mina.MinaIOPlexorTest;
import org.tomac.protocol.fix.messaging.FixHeartbeat;
import org.tomac.protocol.fix.messaging.FixLogon;
import org.tomac.protocol.fix.messaging.FixLogout;
import org.tomac.protocol.fix.messaging.FixMessage;
import org.tomac.protocol.fix.messaging.FixMessageInfo;
import org.tomac.protocol.fix.messaging.FixMessageListener;
import org.tomac.protocol.fix.messaging.FixMessageListenerImpl;
import org.tomac.protocol.fix.messaging.FixNewOrderSingle;
import org.tomac.protocol.fix.messaging.FixReject;
import org.tomac.protocol.fix.messaging.FixResendRequest;
import org.tomac.protocol.fix.messaging.FixSequenceReset;
import org.tomac.protocol.fix.messaging.FixTestRequest;
import org.tomac.utils.Utils;

public class FixClientMina implements IFixApplication{

	private static Logger log = Logger.getLogger(FixClientMina.class.getSimpleName());

	FixMessageListenerImpl inboundFixMessageListener;
	IFixSession session;
	IIOPlexor plex;
	boolean forever = true;
	boolean isSendingOrders = false;

	static public void main(String[] args) {

		if (args.length < 1) {
			System.out.println("Usage: FixClient <fix_url>");
			System.out.println("Example: FixClient fix42nordic://TOMAC:TE@127.0.0.1:1212?heartBtInt=60#initiator");
			return;
		}
		
		BasicConfigurator.configure();

		IFixURL url = new FixURL(args[0]); 

		IIOPlexor plex = new MinaIOPlexorTest();

		IFixApplication app = new FixClientMina();
			
		IFixSession initator = new FixInitiator(plex, url, app);
			
		app.init(initator, plex);
			
	}

	@Override
	public void onDisconnect() {
	}

	@Override
	public void onConnect() {
		log.info("Got connect");
	}

	@Override
	public FixMessageListener getInboundFixMessageReplicator() {
		return inboundFixMessageListener;
	}

	@Override
	public void init(IFixSession session, IIOPlexor plex) {
		this.session = session;
		this.plex = plex;
		inboundFixMessageListener = new FixMessageListenerReplicator(this);
		try {

			session.connect();

		} catch (IOException e) {
			if (log.isDebugEnabled()) log.error(e.getMessage(), e);
			else log.error(e.getMessage());
		}
		
	}
	
	private void sendNewOrderSingle() {
		FixNewOrderSingle order = new FixNewOrderSingle();
		order.price = 1000000;
		order.orderQty = 1000;
		Utils.longToNumeric(order.clOrdID, 0, plex.currentTimeMillis(), order.clOrdID.length);
		order.side = FixMessageInfo.Side.BUY;
		order.minQty = 0;
		Utils.copy("VOLVO".getBytes(), 0, order.symbol, 0, "VOLVO".length());
		order.ordType = FixMessageInfo.OrdType.LIMIT;
		Utils.utcTimestampConverter.convertToUtcTimestamp(order.transactTime, plex.currentTimeMillis(), true);
		order.handlInst = FixMessageInfo.HandlInst.AUTOMATED_EXECUTION_ORDER_PRIVATE_NO_BROKER_INTERVENTION;
		session.send(order);
	}
	
	private class FixMessageListenerReplicator extends FixMessageListenerImpl implements FixMessageListener {
		
		FixClientMina parent;
		
		public FixMessageListenerReplicator(FixClientMina fixApplicationTest) {
			parent = fixApplicationTest;
		}

		@Override
		public void onFixNewOrderSingle(FixNewOrderSingle msg) {
			session.onFixNewOrderSingle(msg); // increment InSeqNo
			parent.sendNewOrderSingle();
		}

		@Override
		public void onUnknownMessageType(FixMessage msg) {
			session.onUnknownMessageType(msg);
		}

		@Override
		public void onFixHeartbeat(FixHeartbeat msg) {
			session.onFixHeartbeat(msg);
			
		}

		@Override
		public void onFixLogon(FixLogon msg) {
			session.onFixLogon(msg);
			parent.sendNewOrderSingle();
		}

		@Override
		public void onFixLogout(FixLogout msg) {
			session.onFixLogout(msg);
			
		}

		@Override
		public void onFixReject(FixReject msg) {
			session.onFixReject(msg);
			
		}

		@Override
		public void onFixResendRequest(FixResendRequest msg) {
			session.onFixResendRequest(msg);
			
		}

		@Override
		public void onFixSequenceReset(FixSequenceReset msg) {
			session.onFixSequenceReset(msg);
			
		}

		@Override
		public void onFixTestRequest(FixTestRequest msg) {
			session.onFixTestRequest(msg);
			
		}
	}
	
	

}
